<template>

  <div class="user d-b-s">
    <div class="left">
      <el-form size="small" :label-width="50" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="职业">
          <el-select size="small" ref="career_id" @change="careerChange" v-model="searchForm.career_id"
            placeholder="请选择">
            <el-option v-for="(item, index) in positionList" :key="index" :label="item.title"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="级别">
          <el-select size="small" ref="level_id" @change="levelChange" v-model="searchForm.level_id" placeholder="请选择">
            <el-option v-for="(item, index) in levelList" :key="index" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方向">
          <el-select size="small" ref="direction_id" v-model="searchForm.direction_id" placeholder="请选择">
            <el-option v-for="(item, index) in directionList" :key="index" :label="item.title"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <div class="appraisal-list">
          <div :class="['app-btn', { 'app-btn-select': item.id === searchForm.appraisal_id }]"
            v-for="(item, i) in appraisalList" :key="i" @click="appChange(item.id, item.appraisal)">{{ item.appraisal }}
          </div>
        </div>
      </el-form>
    </div>
    <div class="right">
      <!--搜索表单-->
      <div class="common-seach-wrap">
        <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="课件名称">
            <el-input size="small" v-model="searchForm.title" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="common-content" v-if="searchForm.appraisal_id && searchForm.direction_id">
        <!--添加管理员-->
        <div class="common-level-rail">
          <el-button size="small" type="primary" @click="addClick" >新增课件</el-button>
        </div>

        <!--内容-->
        <div class="product-content">
          <div class="table-wrap">
            <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
              <el-table-column min-width="120" align="center" prop="title" label="课件名称"></el-table-column>
              <el-table-column min-width="100" align="center" prop="video_type" label="课件类型">
                <template #default="scope">
                  <span>{{ scope.row.type == 1 ? '总结' : '鉴定点' }}</span>
                </template>
              </el-table-column>
              <el-table-column min-width="120" align="center" prop="device_name" label="设备名称"></el-table-column>
              <el-table-column min-width="120" align="center" prop="career_title" label="所属职业"></el-table-column>
              <el-table-column min-width="120" align="center" prop="appraisal_title" label="所属鉴定站"></el-table-column>
              <el-table-column min-width="120" align="center" prop="level_title" label="所属级别"></el-table-column>
              <el-table-column min-width="120" align="center" prop="direction_title" label="所属方向"></el-table-column>
              <el-table-column align="center" fixed="right" label="操作" width="120">
                <template #default="scope">
                  <el-button @click="editClick(scope.row)" type="text" size="small">编辑</el-button>
                  <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <!--分页-->
        <div class="pagination">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
            :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
            :total="totalDataNumber"></el-pagination>
        </div>
      </div>
    </div>



    <Add v-if="open_add" ref="add" :setParams="setParams" @close="closeAdd"></Add>

    <Edit :open="open_edit" :roleList="roleList" :shop_user_id="curModel.shop_user_id" @close="closeEdit"></Edit>
    <el-dialog title="删除课件" style="width: 251px;" modal-class="dialog-add" v-model="dialogVisible"
      @close="dialogVisible = false" :close-on-click-modal="false" :close-on-press-escape="false">
      是否确定要删除课件？
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="delCourseware()" :loading="deleteloading">是</el-button>
          <el-button @click="dialogVisible = false">否</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import PositionApi from '@/api/position.js';
import CourseApi from '@/api/course.js';
import Add from './dialog/Add.vue';
import Edit from './dialog/Edit.vue';
export default {
  components: {
    Add,
    Edit
  },
  inject: ['reload'],
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*角色是否加载完成*/
      role_loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        user: '',
        region: ''
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      curModel: {},
      /*角色列表*/
      roleList: [],
      searchForm: {
        title: '',
        career_id: '',
        level_id: '',
        direction_id: '',
        appraisal_id: '',
      },
      positionList: [], //全部职业
      levelList: [],
      directionList: [],
      appraisalList: [],
      dialogVisible: false,
      deleteloading: false,
      setParams: {},
      course_id: ''
    };
  },
  created() {
    this.getPositionAll()
  },
  methods: {
    careerChange() {
      this.searchForm.level_id = ''
      this.levelList = []
      this.searchForm.direction_id = ''
      this.directionList = []
      this.searchForm.appraisal_id = ''
      this.appraisalList = []
      this.getLevelAll()
      this.getAppraisalAll()
    },
    levelChange() {
      this.searchForm.direction_id = ''
      this.directionList = []
      this.getDirectionAll()
    },
    appChange(id, title) {
      this.searchForm.appraisal_id = id
      this.searchForm.appraisal_title = title
      this.getTableList()
    },
    getPositionAll() {
      PositionApi.getNestedData({}, true)
        .then(res => {
          if (!this.searchForm.career_id) {
            this.searchForm.career_id = res.data && res.data[0] && res.data[0].id ? res.data[0].id : ''
            if (this.searchForm.career_id) {
              this.getLevelAll()
              this.getAppraisalAll()
            }
          }
          this.positionList = res.data;
        })
        .catch(error => { });
    },
    getLevelAll() {
      PositionApi.getCareerLevel({
        career_id: this.searchForm.career_id
      }, true)
        .then(data => {
          this.searchForm.level_id = data.data && data.data[0] && data.data[0].id ? data.data[0].id : ''
          if (this.searchForm.level_id) {
            this.getDirectionAll()
          }
          this.levelList = data.data;

        })
        .catch(error => { });
    },
    getDirectionAll() {
      PositionApi.getCareerDirection({
        career_id: this.searchForm.career_id,
        level_id: this.searchForm.level_id
      }, true)
        .then(data => {
          this.searchForm.direction_id = data.data && data.data[0] && data.data[0].id ? data.data[0].id : ''
          this.getTableList()
          this.directionList = data.data;
        })
        .catch(error => { });
    },
    getAppraisalAll() {
      PositionApi.getCareerAppraisal({
        career_id: this.searchForm.career_id
      }, true)
        .then(data => {
          this.searchForm.appraisal_id = data.data && data.data[0] && data.data[0].id ? data.data[0].id : ''
          this.searchForm.appraisal_title = data.data && data.data[0] && data.data[0].appraisal ? data.data[0].appraisal : ''
          this.getTableList()
          this.appraisalList = data.data;
        })
        .catch(error => { });
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getTableList();
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      if (!this.searchForm.direction_id || !this.searchForm.appraisal_id) {
        return false
      }
      let self = this;
      let Params = {
        ...self.searchForm
      };
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      CourseApi.getCoursewareList(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
        })
        .catch(error => { });
    },

    /*打开添加*/
    addClick() {
      console.log(this.$refs.career_id.selectedLabel)
      console.log(this.$refs.level_id.selectedLabel)
      console.log(this.$refs.direction_id.selectedLabel)
      this.setParams = {
        career_id: this.searchForm.career_id,
        career_title: this.$refs.career_id.selectedLabel,
        level_id: this.searchForm.level_id,
        level_title: this.$refs.level_id.selectedLabel,
        direction_id: this.searchForm.direction_id,
        direction_title: this.$refs.direction_id.selectedLabel,
        appraisal_id: this.searchForm.appraisal_id,
        appraisal_title: this.searchForm.appraisal_title
      }
      this.open_add = true;
    },

    /*关闭添加*/
    closeAdd(e) {
      this.open_add = false;
      if (e && e.type == 'success') {
        this.getTableList();
      }
    },

    /*打开编辑*/
    editClick(row) {
      this.open_add = true;
      this.$nextTick(() => {
        this.$refs.add.setTitle({
          title: row.title,
          id: row.course_id,
          data: row
        })
      })
    },

    /*关闭添加*/
    closeEdit(e) {
      this.open_edit = false;
      if (e && e.type == 'success') {
        this.getTableList();
      }
    },

    /*删除*/
    deleteClick(row) {
      this.course_id = row.course_id;
      this.dialogVisible = true;
    },
    // 确认删除
    delCourseware() {
      let self = this;
      self.deleteloading = true;
      CourseApi.delCourseware(
        {
          course_id: self.course_id
        },
        true
      )
        .then(data => {
          self.deleteloading = false;
          if (data.code == 1) {
            ElMessage({
              message: '删除成功',
              type: 'success'
            })
            self.getTableList();
            this.dialogVisible = false;
          } else {
            self.deleteloading = false;
          }
        }).catch(error => {
          self.deleteloading = false;
        })
    }
  }
};
</script>

<style lang="scss" scoped>
.user {
  width: 100%;
  min-height: calc(100vh - 90px);

  .left {
    padding: 20px 0 20px 20px;
    background: #fff;
    min-width: 266px;
    width: 266px;
    margin-right: 13px;
    height: calc(100vh - 90px);
    overflow-y: auto;
  }

  .right {
    overflow-y: auto;
    height: calc(100vh - 90px);
    width: calc(100% - 279px);
  }
}

.appraisal-list {
  width: 100%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  overflow-y: auto;
}

.app-btn {
  cursor: pointer;
  width: 216px;
  min-height: 31px;
  background: #FFFFFF;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #DCDCDC;
  margin-bottom: 13px;
  text-align: center;
  line-height: 31px;
  overflow: hidden;
  /* 确保超出容器的文本被裁剪 */
  white-space: nowrap;
  /* 确保文本在一行内显示 */
  text-overflow: ellipsis;
  /* 使用省略号表示文本溢出 */
}

.app-btn-select {
  border: 1px solid #3A6AE2;
  color: #3A6AE2;
}
</style>